/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-12-16 13:48:49
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../extends/timeFormart");
class AuditService extends Service {
    //获取列表
    async getAuditList() {
            const { ctx, app } = this;
            let res = await app.mysql.select("audit");
            res = res.map((item, index) => {
                return {
                    ...item,
                    key: index + 1,
                };
            });
            return res;
        }
        //搜索
    async searchAuditList() {
            const { ctx, app } = this;
            const { address, keyword, state } = ctx.query;
            let res = await app.mysql.select("audit");
            if (keyword) {
                res = res.filter((item) => item.name.includes(keyword) || item.title.includes(keyword));
            }
            if (address) {
                res = res.filter((item) => item.address.includes(address));
            }
            if (state !== undefined) {
                res = res.filter((item) => item.state === state);
            }
            res = res.map((item, index) => {
                return {
                    ...item,
                    key: index + 1,
                };
            });
            return res;
        }
        //删除
    async delAuditList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("audit", { id });
            return res;
        }
        //编辑
    async editAuditList() {
            const { ctx, app } = this;
            const { id, name, title, area, total_price, sort, state, address, detail, house_type } = ctx.request.body;
            const res = await app.mysql.update("audit", { id, ...ctx.request.body });
            return res;
        }
        //改变审核状态
    async changeAuditState() {
        const { ctx, app } = this;
        const { id } = ctx.request.body;
        const res = await app.mysql.get("audit", { id });
        const state = res.state - 0;
        const result = await app.mysql.update("audit", { id, state: !state });
        return result;
    }
}
module.exports = AuditService;